package com.zhida.smartcontrol.common.mapsel;

import com.alibaba.fastjson.JSON;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.LogUtil;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

public class MapSelPresenter extends BasePresenterImpl<MapSelContract.View> implements MapSelContract.Presenter {

    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;

    @Override
    public void initBaiduMap(BaiduMap baiduMap) {
        mBaiduMap = baiduMap;

        // 设置地图缩放级别
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(15.0f);

        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        //定位初始化
        mLocationClient = new LocationClient(mView.getContext());
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(30 * 1000); // 定位时间间隔，单位ms
        mLocationClient.setLocOption(option);

        mLocationClient.registerLocationListener(new MyLocationListener());
        //开启地图定位图层
        mLocationClient.start();

        setMyLocationEnabled(true);
    }

    @Override
    public void startLocation() {
        if (null != mLocationClient) {
            mLocationClient.start();
        }
    }

    @Override
    public void stopLocation() {
        if (null != mLocationClient) {
            mLocationClient.stop();
        }
    }

    @Override
    public void setMyLocationEnabled(boolean enabled) {
        if (null != mBaiduMap) {
            mBaiduMap.setMyLocationEnabled(enabled);
        }
    }

    private class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null) {
                return;
            }

            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

            final CircleOptions ooCircle = new CircleOptions().fillColor(0x303862FF)
                    .center(latLng)
                    .radius(DimenUtils.dp2px(mView.getContext(), 96));
            mBaiduMap.addOverlay(ooCircle);

            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_location_dot_blue);

            OverlayOptions option = new MarkerOptions().position(latLng).icon(bitmap);
            mBaiduMap.addOverlay(option);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLng));

            findAddress(latLng.latitude, latLng.longitude);
        }
    }

    @Override
    public void findAddress(double lat, double lng) {
        LogUtil.e(TAG, lat + "," + lng);

        String url = "http://api.map.baidu.com/reverse_geocoding/v3/?";
        Params params = Params.newBuilder()
                .add("ak", "ZvOCuSg8TLohK0qmBoyY6jWnWhNnsiBo")
                .add("mcode", "7B:6A:B7:6D:9C:A6:E2:75:7F:A1:2B:A8:78:E6:FE:50:4D:34:99:51;com.zhida.smartcontrol")
                .add("output", "json")
                .add("coordtype", "bd09ll")
                .add("location", lat + "," + lng)
                .build();
        mKalleCenter.get(url, params, true, true, new KalleCallBack() {
            @Override
            public void onSuccess(String json, String msg) {
                BaiduAddressBean bean = JSON.parseObject(json).toJavaObject(BaiduAddressBean.class);
                if (bean.getStatus() == 0) {
                    mView.showLatLng(bean.getResult());
                }
            }

            @Override
            public void onFailed(String msg) {

            }
        });

    }

}
